package admin

import (
	"github.com/gin-gonic/gin"
	"math"
	"net/http"
	"strconv"
	"time"
	. "xm_shop/database"
	. "xm_shop/models"
)

type NavController struct{}

// 查询全部导航栏
func (n NavController) Index(context *gin.Context) {
	// 分页
	page, err1 := strconv.Atoi(context.Query("page"))
	if err1 != nil || page == 0 {
		page = 1
	}
	pageSize := 8
	var navList []Nav
	DB.Offset((page - 1) * pageSize).Limit(pageSize).Find(&navList)
	// 获取总数量
	var count int64
	DB.Table("nav").Count(&count)
	context.JSON(http.StatusOK, gin.H{
		"success":    true,
		"data":       navList,
		"count":      count,
		"totalPages": math.Ceil(float64(count) / float64(pageSize)),
		"page":       page,
	})
}

// 新增
func (n NavController) Add(context *gin.Context) {
	title := context.PostForm("title")
	link := context.PostForm("link")
	position, err1 := strconv.Atoi(context.PostForm("position"))
	isOpenNew, err2 := strconv.Atoi(context.PostForm("is_open_new"))
	relation := context.PostForm("relation")
	sort, err3 := strconv.Atoi(context.PostForm("sort"))
	status, err4 := strconv.Atoi(context.PostForm("status"))
	if err1 != nil || err2 != nil || err3 != nil || err4 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	if title == "" {
		context.String(http.StatusBadRequest, "标题不能为空")
		return
	}
	// 创建记录
	nav := Nav{
		Title:     title,
		Link:      link,
		Position:  position,
		IsOpenNew: isOpenNew,
		Relation:  relation,
		Sort:      sort,
		Status:    status,
		AddTime:   int(time.Now().Unix()),
	}
	if err5 := DB.Create(&nav).Error; err5 != nil {
		context.String(http.StatusBadRequest, "新增失败")
		return
	}
	context.String(http.StatusOK, "新增成功")
}

// 编辑
func (n NavController) Edit(context *gin.Context) {
	id, err5 := strconv.Atoi(context.Param("id"))
	title := context.PostForm("title")
	link := context.PostForm("link")
	position, err1 := strconv.Atoi(context.PostForm("position"))
	isOpenNew, err2 := strconv.Atoi(context.PostForm("is_open_new"))
	relation := context.PostForm("relation")
	sort, err3 := strconv.Atoi(context.PostForm("sort"))
	status, err4 := strconv.Atoi(context.PostForm("status"))
	if err1 != nil || err2 != nil || err3 != nil || err4 != nil || err5 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	if title == "" {
		context.String(http.StatusBadRequest, "标题不能为空")
		return
	}
	// 检索数据
	var nav Nav
	DB.Where("id = ?", id).Take(&nav)
	nav.Title = title
	nav.Link = link
	nav.Position = position
	nav.IsOpenNew = isOpenNew
	nav.Relation = relation
	nav.Sort = sort
	nav.Status = status
	if err6 := DB.Save(&nav).Error; err6 != nil {
		context.String(http.StatusBadRequest, "修改失败")
		return
	}
	context.String(http.StatusOK, "修改成功")
}

// 检索特定导航栏
func (n NavController) GetOnlyNav(context *gin.Context) {
	id, err1 := strconv.Atoi(context.Param("id"))
	if err1 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	// 检索
	var nav Nav
	DB.Where("id = ?", id).Take(&nav)
	context.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    nav,
	})
}

// 删除
func (n NavController) Delete(context *gin.Context) {
	id, err1 := strconv.Atoi(context.Param("id"))
	if err1 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	// 检索
	var nav Nav
	DB.Where("id = ?", id).Take(&nav)
	if err2 := DB.Delete(&nav).Error; err2 != nil {
		context.String(http.StatusBadRequest, "删除失败")
		return
	}
	context.String(http.StatusOK, "删除成功")
}
